﻿namespace Manga.Api
{
    using System;
    using System.Threading.Tasks;
    using HtmlAgilityPack;
    using Windows.Web.Http;
    public class ApiBase
    {
        private readonly HttpClient _Serivce = new HttpClient();
        private readonly HtmlDocument _Document = new HtmlDocument();
        async protected Task<bool> Get(string uri)
        {
            try
            {
                var content = await _Serivce.GetStringAsync(new Uri(uri));
                _Document.LoadHtml(content);
                return true;
            }
            catch (UriFormatException ufe)
            {

                System.Diagnostics.Debug.WriteLine(ufe.Message);
                System.Diagnostics.Debug.WriteLine(uri);
                return false;
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
        }

        protected HtmlNode Single(string xpath)
        {
            return _Document.DocumentNode.SelectSingleNode(xpath);
        }
        protected HtmlNode Single(HtmlNode node,string xpath)
        {
            return node.SelectSingleNode(xpath);
        }
        protected HtmlNodeCollection Multi(string xpath)
        {
            return _Document.DocumentNode.SelectNodes(xpath);
        }
        protected HtmlNodeCollection Multi(HtmlNode node,string xpath)
        {
            return node.SelectNodes(xpath);
        }
    }
}
